package com.lp.easymethodlink;

import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.SelectionModel;
import com.intellij.openapi.ui.Messages;

public class RightMenuPopText extends AnAction {

    @Override
    public void actionPerformed(AnActionEvent e) {
        // 获取当前编辑器
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        if (editor != null) {
            // 获取选择模型
            SelectionModel selectionModel = editor.getSelectionModel();
            String selectedText = selectionModel.getSelectedText();
            
            if (selectedText != null && !selectedText.isEmpty()) {
                Messages.showInfoMessage("选中的文本为：" + selectedText, "选中的文本");
            } else {
                Messages.showInfoMessage("没有选中文本", "提示");
            }
        }
    }
    
    @Override
    public void update(AnActionEvent e) {
        // 在更新方法中设置操作是否可用
        Editor editor = e.getData(CommonDataKeys.EDITOR);
        e.getPresentation().setEnabledAndVisible(editor != null && editor.getSelectionModel().hasSelection());
    }
}